<?php


namespace App\Http\Controllers\Wechat;
use App\Models\ActivityPage;
use App\Models\AppletsIndex;

use App\Models\AppletsIndexGoodsList;
use App\Models\AppletsIndexListCategory;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class IndexController
{
    /**
     * 小程序首页数据
     * @param Request $request
     * @return false|string
     */
    public function index(Request $request){
        $page = $request->input('page',1);
        $pageSize = AppletsIndex::CACHE_LIMIT_NUM;
        $data = Redis::hget(AppletsIndex::REDIS_KEY,$page);
        if($data){
            $list = json_decode($data,true);
        }else{
            $limitData = AppletsIndex::where(['status'=>AppletsIndex::STATUS_YES_SHOW,'is_release'=>AppletsIndex::RELEASE_ONLINE_YES])
                ->orderBy('type','asc')
                ->paginate($pageSize);
            $list = obToArray($limitData);
            if($list){
                Redis::hset(AppletsIndex::REDIS_KEY,$page,json_encode($list));
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }

    /**
     * 首页推荐分类列表
     * @return false|string
     */
    public function listCategoryList(){
        $category = Redis::get(AppletsIndexListCategory::REDIS_CATEGORY_KEY);
        $category = json_decode($category,true);
        if(empty($category)){
            $category = AppletsIndexListCategory::getList();
            if($category){
                Redis::set(AppletsIndexListCategory::REDIS_CATEGORY_KEY,json_encode($category));
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$category);
    }

    /**
     * 首页推荐分类商品列表
     * @param Request $request
     * @return false|string
     */
    public function indexList(Request $request){
        $cateId = $request->input('cateId','');
        $page = $request->input('page','1');
        $pageSize = $request->input('pageSize',AppletsIndexGoodsList::CACHE_LIMIT_NUM);
        $list = [];
        if(empty($cateId)){
            $category = Redis::get(AppletsIndexListCategory::REDIS_FIRST_CATEGORY_KEY);
            $category = json_decode($category,true);
            if(empty($category)){
                $category = AppletsIndexListCategory::getFirstList();
            }
            if($category){
                $cateId = $category['id'];
            }
        }
        if(!empty($cateId)){
            $list = Redis::hget(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page);
            if($list){
                $list = json_decode($list,true);
            }else{
                $list = AppletsIndexGoodsList::getList($cateId,$page,$pageSize);
                if($list){
                    Redis::hset(AppletsIndexGoodsList::REDIS_LIST_KEY,$cateId.'-'.$page,json_encode($list));
                }
            }
        }

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }



    //根据id获取活动分类 http://shop.cn/Alipay/Index/getActivityPageById?id=
    public function getActivityPageById(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $data = Redis::hget(ActivityPage::ACTIVITY_PAGE_KEY, $input['id']);
        if ($data) {
            $data = json_decode($data, true);
        } else {
            $data = ActivityPage::GetOneActivityPage(array('id' => $input['id'],'status' => ActivityPage::ACTIVITY_PAGE_STATUS_PUBLISHED));
            if (!empty($data)) {
                Redis::hset(ActivityPage::ACTIVITY_PAGE_KEY, $input['id'], json_encode($data));
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

}
